<div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
    <h4 class="modal-title" id="addModalLabel">新增用户组</h4>
</div>
<div class="modal-body">
    <form id="add-group-form">
    <div class="form-group input-group  input-control">
        <span class="input-group-addon">组名&nbsp;</span>
        <input type="text" class="form-control" id="gname" name="gname" placeholder="" value="<?php echo $gname; ?>">
        <input type="hidden" id="gid" name="gid" value="<?php echo $gid; ?>">
        <input type="hidden" id="ogname" name="ogname" value="<?php echo $gname; ?>">
    </div>
    <span class="span-name" id="exist-tips"></span>

    <div class="form-group input-group  input-control">
        <span class="input-group-addon">简介&nbsp;&nbsp;</span>
        <textarea class="form-control" id="description" name="description" rows="3"><?php echo $description; ?></textarea>
    </div>
    </form>
</div>
<div class="modal-footer">
    <span class="tips" id="atip"></span>
    <button type="button" id="cancel" class="btn btn-default" data-dismiss="modal">&nbsp;&nbsp;取消&nbsp;&nbsp;</button>
    <button type="button" id="add" class="btn btn-primary">&nbsp;&nbsp;确认&nbsp;&nbsp;</button>
</div>
<script>
$('#add').click(function(){
    var name = $('#gname').val();

    if (!name.trim()) {
        alert('请填写组名');
        return false;
    }

    $.post(url('/group/savegroup'), $('#add-group-form').serialize(), function(ret){
        if (ret.retcode!=2000000) {
            alert(ret.msg);
        } else {
            alert('保存成功');
            refresh();
        }
    }, 'json');
});

$('#gname').blur(function(){
    var name    = $('#gname').val();
    var ogname  = $('#ogname').val();

    if (!name || ogname === name) {
        return false;
    }

    $.post(url('/group/exist'), {gname:name}, function(ret){
        if (ret && ret.retcode != 2000000) {
            $('#exist-tips').text(name + ' 已存在');
            $(this).focus();
        }else{
            $('#exist-tips').html('');
        }
    }, 'json');
});
</script>
